//
//  ICSegmentView.m
//  IdealerClub
//
//  Created by David on 16/1/13.
//  Copyright © 2016年 IC. All rights reserved.
//

#import "ICSegmentView.h"

@interface ICSegmentView()
@property(nonatomic, strong)NSMutableArray *buttons;
@property(nonatomic, strong)UIView *bottomLine;
@property(nonatomic, copy)ClickSegmentViewBlock block;
@end

@implementation ICSegmentView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = kGreenColor;
        self.buttons = [NSMutableArray array];
    }
    return self;
}

- (void)setSegmentArray:(NSArray *)array SelectIndex:(ClickSegmentViewBlock)block{
    
    self.block = block;
    
    for (UIButton *btn in self.buttons) {
        [btn removeFromSuperview];
    }
     [self.buttons removeAllObjects];
    NSInteger nCount = array.count;
    self.bottomLine.width = [self getWidthWithTitle:array[0]];//(self.frame.size.width - 75.f)/nCount;
    CGFloat x = 25;
    for (int i = 0; i < nCount; i++) {
        CGFloat width = [self getWidthWithTitle:array[i]];
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(x, 0, width, self.height)];
        [btn setTitle:array[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        btn.titleLabel.font = [UIFont systemFontOfSize:13.f];
        x += width + 25;
        if (i == 0) {
            btn.selected = YES;
            self.bottomLine.centerX = btn.centerX;
            self.bottomLine.bottom = btn.bottom - 10;
        }
        [btn addTarget:self action:@selector(onClickSegment:)forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        [self.buttons addObject:btn];
    }
    [self addSubview:self.bottomLine];
}

- (void)onClickSegment:(UIButton *)btn{
    NSInteger index = [self.buttons indexOfObject:btn];
    self.bottomLine.width = btn.width;
    if (self.block) {
        self.block(index);
    }
    //移动UIScrollView
    CGFloat originX = btn.center.x - CGRectGetMidX(self.frame);
    CGFloat maxOffsetX = self.contentSize.width - self.frame.size.width;
    if (originX < 0) {
        originX = 0;
    }else if (originX > maxOffsetX){
        originX = maxOffsetX;
    }
    [self setContentOffset:CGPointMake(originX, 0) animated:YES];
    
    [UIView animateWithDuration:0.3f animations:^{
        self.bottomLine.centerX = btn.centerX;
    } completion:^(BOOL finished) {
        for (int i = 0; i < self.buttons.count; i++) {
            UIButton *tempBtn = self.buttons[i];
            tempBtn.selected = NO;
        }
        btn.selected = YES;
    }];

}
- (void)setSeletIndex:(NSInteger)index{
    for (int i = 0; i < self.buttons.count; i++) {
        UIButton *tempBtn = self.buttons[i];
        tempBtn.selected = NO;
    }
    
    UIButton *btn = self.buttons[index];
    btn.selected = YES;
    //移动UIScrollView
    
    CGFloat originX = btn.center.x - CGRectGetMidX(self.frame);
    CGFloat maxOffsetX = self.contentSize.width - self.frame.size.width;
    if (originX < 0) {
        originX = 0;
    }else if (originX > maxOffsetX){
        originX = maxOffsetX;
    }
    [self setContentOffset:CGPointMake(originX, 0) animated:YES];

    
    if (self.block) {
        self.block(index);
    }
    
    self.bottomLine.width = btn.width;
     self.bottomLine.centerX = btn.centerX;
    //自适应宽度
    self.bottomLine.width = btn.width;
}

- (UIView *)bottomLine{
    if (!_bottomLine) {
        _bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 2.f)];
        _bottomLine.backgroundColor = [UIColor whiteColor];
    }
    return _bottomLine;
}

- (CGFloat)getWidthWithTitle:(NSString *)title{
    UILabel *tempLabel = [[UILabel alloc] init];
    tempLabel.font = [UIFont systemFontOfSize:13];
    tempLabel.text = title;
    [tempLabel sizeToFit];
    
    return tempLabel.width;
}

@end
